﻿#include "EntryCombobox.h"
#include "LumDebug.h"
#include <QWheelEvent>
#include <QListView>

const QString combobox_qss = R"(
    QComboBox {
        border-radius: 3px;
        background: #46433E;
        color: #FFFFFF;
    }

    QComboBox:on {
        border-bottom-left-radius: 0px;
        border-bottom-right-radius: 0px;
    }

    QComboBox::drop-down {
        background-color: transparent;
    }

    QComboBox QListView {
        border-radius: 6px;
        border-top-left-radius: 0px;
        border-top-right-radius: 0px;
        color: #FFFFFF;
        outline: none;
        border: 1px solid #3A3A3A;
        background-color: #2B2B2B;
        padding: 6px 6px 6px 6px;
    }

    QComboBox QListView::item {
        border-radius: 3px;

        min-height: 20px;
        max-height: 20px;

    }

    QComboBox QListView::item:hover {
        background-color: #393939;
    }

    QComboBox QListView::item:selected {
        background-color: #383838;
    }

    QComboBox QListView::item:hover:selected {
        background-color: #373737;
    }
)";

EntryCombobox::EntryCombobox(QWidget *parent) : QComboBox{parent} {
    setFocusPolicy(Qt::StrongFocus);
    //setStyleSheet(combobox_qss);

    setView(new QListView);

    const auto popup = view()->topLevelWidget();
    popup->setWindowFlag(Qt::FramelessWindowHint);
    popup->setWindowFlag(Qt::NoDropShadowWindowHint);
    popup->setAttribute(Qt::WA_TranslucentBackground);
}

void EntryCombobox::setCurrentData(const QVariant &value, int role) {
    for(int i = 0; i < count(); ++i) {
        const auto &data = itemData(i, role);
        if(data == value) {
            setCurrentIndex(i);
            return ;
        }
    }
}

void EntryCombobox::showPopup() {
    QComboBox::showPopup();

    auto boxRect = rect();
    boxRect.moveTopLeft(mapToGlobal(QPoint(0,0)));

    const auto popup = view()->topLevelWidget();
    auto pupupRect = popup->geometry();
    pupupRect.setWidth(width());

    pupupRect.moveTopLeft(boxRect.bottomLeft() + QPoint(0, 0));
    popup->setGeometry(pupupRect);
}

void EntryCombobox::wheelEvent(QWheelEvent *event) {
    if(hasFocus()) {
        QComboBox::wheelEvent(event);
    } else {
        event->ignore();
    }
}
